﻿using System;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using Yhd.Captcha.Core.OtpNet;

namespace Zt.QmtqDemo.ConfmPlan.Saga
{

    
    public class ConfmPlanTask : StepBody, ITransientDependency
    {
        public List<ConfmMatDto> MatDtos { get; set; }

        public string Message { get; set; }

        public int Code { get; set; }

        public Guid PlanId { get; set; }

        public string PlanCode { get; set; }

        public string OrderNo  { get;set; }

        private IConformPlanRepository conformPlanRepository;


        public ConfmPlanTask (IConformPlanRepository planRepository)
        {
            this.conformPlanRepository = planRepository;
        }

        public override ExecutionResult Run(IStepExecutionContext context)
        {
            try
            {

                var conformPlan = new ConformPlan(PlanId, this.PlanCode, OrderNo);

                foreach (var item in MatDtos)
                {
                    conformPlan.AddConformPlanItem(this.PlanCode, item.MatNo, item.MatThick, item.MatWidth, item.MatLen, item.MatWt);
                }

                conformPlan.SumTotalMatNum();
                conformPlan.SumTotalMatWt();

                conformPlanRepository.InsertAsync(conformPlan, autoSave: true);
            }
           catch(Exception ex)
            {

            }
            return ExecutionResult.Next();
        }
    }
}
